package com.srwl.mytx.tao.av;


import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.os.Build;
import android.os.Build.VERSION;
import android.text.TextUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashSet;
import java.util.regex.Pattern;

public class SystemUtil {
    public static Application sApplication;
    private static String sModel;
    private static final String[] BLACK_LIST_OF_FOCUS_MODE_CHANGE = new String[]{"GT-I9500"};
    public static HashSet<String> CAMERA_FACING_OPPOSITE_MODELS = new HashSet(Arrays.asList("MI 2S"));
    public static HashSet<String> CAMERA_FACING_FRONT_90_MODELS = new HashSet(Arrays.asList("Nexus 6P"));

    public SystemUtil() {
    }

    private static int getNumCores() {
        try {
            File dir = new File("/sys/devices/system/cpu/");

            class CpuFilter implements FileFilter {
                CpuFilter() {
                }

                public boolean accept(File pathname) {
                    return Pattern.matches("cpu[0-9]", pathname.getName());
                }
            }

            File[] files = dir.listFiles(new CpuFilter());
            return files.length;
        } catch (Exception var2) {
            var2.printStackTrace();
            return 1;
        }
    }

    private static int getCpuNum() {
        int cpuNum = Runtime.getRuntime().availableProcessors();
        return cpuNum;
    }

    private static long getMaxCpuFreq() {
        String result = "";

        try {
            String[] args = new String[]{"/system/bin/cat", "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"};
            ProcessBuilder cmd = new ProcessBuilder(args);
            Process process = cmd.start();
            InputStream in = process.getInputStream();

            for(byte[] re = new byte[24]; in.read(re) != -1; result = result + new String(re)) {
            }

            in.close();
        } catch (IOException var7) {
            var7.printStackTrace();
            result = "N/A";
        }

        long maxCpuFreq = 0L;

        try {
            maxCpuFreq = (long)(Integer.valueOf(result.trim()) / 1024);
        } catch (Exception var6) {
        }

        return maxCpuFreq;
    }

    private static String getMinCpuFreq() {
        String result = "";

        try {
            String[] args = new String[]{"/system/bin/cat", "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq"};
            ProcessBuilder cmd = new ProcessBuilder(args);
            Process process = cmd.start();
            InputStream in = process.getInputStream();

            for(byte[] re = new byte[24]; in.read(re) != -1; result = result + new String(re)) {
            }

            in.close();
        } catch (IOException var6) {
            var6.printStackTrace();
            result = "N/A";
        }

        return result.trim();
    }

    private static String getCurCpuFreq() {
        String result = "N/A";

        try {
            FileReader fr = new FileReader("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq");
            BufferedReader br = new BufferedReader(fr);
            String text = br.readLine();
            result = text.trim();
            br.close();
        } catch (FileNotFoundException var4) {
            var4.printStackTrace();
        } catch (IOException var5) {
            var5.printStackTrace();
        }

        return result;
    }

    private static String getCpuName() {
        try {
            FileReader fr = new FileReader("/proc/cpuinfo");
            BufferedReader br = new BufferedReader(fr);
            String text = br.readLine();
            String[] array = text.split(":\\s+", 2);

            for(int i = 0; i < array.length; ++i) {
            }

            br.close();
            return array[1];
        } catch (FileNotFoundException var5) {
            var5.printStackTrace();
        } catch (IOException var6) {
            var6.printStackTrace();
        }

        return null;
    }

    private static long getAvailMemory(Context context) {
        ActivityManager am = (ActivityManager)context.getSystemService("activity");
        MemoryInfo mi = new MemoryInfo();
        am.getMemoryInfo(mi);
        return mi.availMem / 1048576L;
    }

    @TargetApi(16)
    public static long getTotalMemory(Context context) {
        long totalMemory = 0L;
        if (VERSION.SDK_INT < 16) {
            totalMemory = getTotalMemoryOld();
        } else {
            ActivityManager am = (ActivityManager)context.getSystemService("activity");
            MemoryInfo mi = new MemoryInfo();
            am.getMemoryInfo(mi);
            totalMemory = mi.totalMem / 1048576L;
        }

        return totalMemory;
    }

    private static long getTotalMemoryOld() {
        String str1 = "/proc/meminfo";
        long initial_memory = 0L;

        try {
            FileReader localFileReader = new FileReader(str1);
            BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);
            String str2 = localBufferedReader.readLine();
            String[] arrayOfString = str2.split(" ");
            String[] arr$ = arrayOfString;
            int len$ = arrayOfString.length;

            for(int i$ = 0; i$ < len$; ++i$) {
                String num = arr$[i$];
                if (num != null && !num.equals("")) {
                    try {
                        initial_memory = (long)(Integer.valueOf(num) / 1024);
                        break;
                    } catch (Exception var12) {
                    }
                }
            }

            localBufferedReader.close();
        } catch (IOException var13) {
        }

        return initial_memory;
    }

    public static boolean isLowPhone(Context context) {
        return getNumCores() < 2 || getTotalMemory(context) < 800L;
    }

    public static boolean isSpecialMobileType() {
        if (TextUtils.isEmpty(Build.MODEL)) {
            return false;
        } else if (Build.MODEL.equals("ZTE U930")) {
            return true;
        } else if (Build.MODEL.contains("GT-S7568")) {
            return true;
        } else {
            return Build.MODEL.contains("HUAWEI C8816");
        }
    }

    public static boolean isArmV7CpuType() {
        if (TextUtils.isEmpty(Build.CPU_ABI)) {
            return false;
        } else {
            return Build.CPU_ABI.contains("armeabi-v7a");
        }
    }

    public static boolean isApkDebugable() {
        if (sApplication == null) {
            return false;
        } else {
            try {
                ApplicationInfo info = sApplication.getApplicationInfo();
                return (info.flags & 2) != 0;
            } catch (Exception var1) {
                var1.printStackTrace();
                return false;
            }
        }
    }

    public static boolean isMobileInFocusModeBlackList() {
        String[] arr$ = BLACK_LIST_OF_FOCUS_MODE_CHANGE;
        int len$ = arr$.length;

        for(int i$ = 0; i$ < len$; ++i$) {
            String model = arr$[i$];
            if (TextUtils.isEmpty(Build.MODEL)) {
                return false;
            }

            if (Build.MODEL.contains(model)) {
                return true;
            }
        }

        return false;
    }

    public static int getCameraFacingBack() {
        return !CAMERA_FACING_OPPOSITE_MODELS.contains(getBuildModel()) ? 0 : 1;
    }

    public static int getCameraFacingFront() {
        return !CAMERA_FACING_OPPOSITE_MODELS.contains(getBuildModel()) ? 1 : 0;
    }

    public static int getCameraFacingFrontDegree() {
        return !CAMERA_FACING_FRONT_90_MODELS.contains(getBuildModel()) ? 270 : 90;
    }

    public static String getBuildModel() {
        if (sModel != null) {
            return sModel;
        } else {
            sModel = Build.MODEL;
            return sModel;
        }
    }
}

